package followme.yaohuile.com.followme.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.mob.tools.gui.ScrollableListView;

import java.util.List;

import followme.yaohuile.com.followme.R;
import followme.yaohuile.com.followme.adapter.recommend.destination.HotCountryAdapter;
import followme.yaohuile.com.followme.adapter.recommend.destination.OtherCountryAdapter;
import followme.yaohuile.com.followme.bean.recommend.destination.CountryInfo;
import followme.yaohuile.com.followme.bean.recommend.destination.DestinationInfo;
import followme.yaohuile.com.followme.interfaceinfo.destination.GetInfo;
import followme.yaohuile.com.followme.network.recommend.destination.DestinationData;

public class DestinationFragment extends Fragment implements AdapterView.OnItemClickListener{

	private View view;
	public static final String urlPost = "http://open.qyer.com/place/common/get_all_country";
	private List<CountryInfo> hotcountrylist;
	private GridView hot_des;
	public static int ant = 0;
	private List<CountryInfo> countrylist;
	private GridView other_des;
	private BitmapUtils bitmapUtils;
	private RadioGroup rg;
	private TextView text_des_hot;
	private TextView text_des_other;
	private DestinationInfo destinationInfo;
	private ScrollView des_scrollview;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_destination, container,false);
		initView(view);
		getDataDes(ant);
		if(ant == 1){
			((RadioButton)rg.getChildAt(5)).setChecked(true);
		}else if(ant == 2){
			((RadioButton)rg.getChildAt(2)).setChecked(true);
		}else if(ant == 3){
			((RadioButton)rg.getChildAt(0)).setChecked(true);
		}else if(ant == 4){
			((RadioButton)rg.getChildAt(1)).setChecked(true);
		}else if(ant == 5){
			((RadioButton)rg.getChildAt(6)).setChecked(true);
		}else if(ant == 6){
			((RadioButton)rg.getChildAt(3)).setChecked(true);
		}else if(ant == 7){
			((RadioButton)rg.getChildAt(4)).setChecked(true);
		}
		bitmapUtils = new BitmapUtils(getActivity());
		hot_des.setOnItemClickListener(this);
		other_des.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				String flag = countrylist.get(position).getFlag();
				if ("2".equals(flag)) {
//					Intent intent = new Intent(getActivity(),DesHotCityActivity.class);
//					String pid = countrylist.get(position).getPid();
//					intent.putExtra("cityId", pid);
//					startActivity(intent);
				} else {
//					Intent intent = new Intent(getActivity(),ShowDesActivity.class);
//					String pid = countrylist.get(position).getPid();
//					intent.putExtra("id", pid);
//					Log.d("main", pid);
//					startActivity(intent);

				}

			}
		});
		return view;
	}

	private void initView(View view) {
		hot_des = (GridView) view.findViewById(R.id.hot_des);
		other_des = (GridView) view.findViewById(R.id.other_des);
		rg = (RadioGroup) view.findViewById(R.id.des_rg);
		text_des_hot = (TextView) view.findViewById(R.id.text_des_hot);
		text_des_other = (TextView) view.findViewById(R.id.text_des_other);
		des_scrollview = (ScrollView) view.findViewById(R.id.des_scrollview);

		rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
					case R.id.Asia:
						ant = 1;
						break;
					case R.id.Europe:
						ant = 2;
						break;
					case R.id.NAmerica:
						ant = 3;
						break;
					case R.id.SAmerica:
						ant = 4;
						break;
					case R.id.Oceania:
						ant = 5;
						break;
					case R.id.Africa:
						ant = 6;
						break;
					case R.id.Antarctica:
						ant = 7;
						break;
				}
				getDataDes(ant);
//				hot_des.setVisibility(View.GONE);
//				other_des.setVisibility(View.GONE);
//				text_des_hot.setVisibility(View.GONE);
//				text_des_other.setVisibility(View.GONE);
			}
		});
	}

	/**
	 * 使用接口回调获取数据
	 * @param i
	 */
	private void getDataDes(final int i) {
		String data = null;
		DestinationData.getData(urlPost, new GetInfo() {
			@Override
			public void sendInfo(String json) {
				Log.d("main", json);
				if (json != null) {
					getString(json, i);
//					hot_des.setVisibility(View.VISIBLE);
//					other_des.setVisibility(View.VISIBLE);
//					text_des_hot.setVisibility(View.VISIBLE);
//					text_des_other.setVisibility(View.VISIBLE);
				}
			}
		});
	}
	private void getString(String json,int i) {
		destinationInfo = new Gson().fromJson(json, DestinationInfo.class);
		hotcountrylist = destinationInfo.getData().get(i).getHotcountrylist();
		countrylist = destinationInfo.getData().get(i).getCountrylist();
		if (i==0){
			text_des_hot.setText("最热门目的地");
			text_des_other.setText("其他热门城市");
		}else{
			text_des_hot.setText(destinationInfo.getData().get(i).getCatename()+"热门目的地");
			text_des_other.setText(destinationInfo.getData().get(i).getCatename()+"其他目的地");
		}

		HotCountryAdapter adapter = new HotCountryAdapter(getActivity(),hotcountrylist,bitmapUtils);
		hot_des.setAdapter(adapter);
		OtherCountryAdapter countryAdapter = new OtherCountryAdapter(getActivity(),countrylist,bitmapUtils);
		other_des.setAdapter(countryAdapter);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		String flag = hotcountrylist.get(position).getFlag();
		if ("2".equals(flag)){
//			Intent intent = new Intent(getActivity(),DesHotCityActivity.class);
//			String pid = hotcountrylist.get(position).getPid();
//			intent.putExtra("cityId", pid);
//			startActivity(intent);
		}else {
//			Intent intent = new Intent(getActivity(),ShowDesActivity.class);
//			String pid = hotcountrylist.get(position).getPid();
//			intent.putExtra("id",pid);
//			Log.d("main", pid);
//			startActivity(intent);
		}
	}
}
